import uuid
from datetime import datetime
import pymysql
from shared.dp_config import DB_CONFIG_PROD
from logger import log_message, log_to_file

# // 测试用机器人
# TEST("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8ea0a080-956d-4f3c-b43e-72e9a75e60de", "企业微信测试机器人"),
#
# / ** ** ** ** ** ** ** ** *深圳配置 ** ** ** ** ** ** ** ** ** ** ** /
# // 外部通知统计机器人 - - 外部告警群
# WE_COM_NOTICE_FAIL("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0832aae3-286a-4788-a205-2613716d225b",
#                    "外部通知统计机器人"),
# // 深圳
# 内部通知机器人 - - 运营群
# SZ_CUS_TARGET_NOT_ENOUGH("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bed72683-5f9d-4b8c-92ca-285208979587",
#                          "内部通知机器人"),
# // 深圳
# 内部通知统计机器人 - - 内部统计告警群 - 领导群
# INNER_NOTICE_ROBOT("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6a240731-9522-42da-b349-adae6600bfb2",
#                    "内部通知统计机器人"),


# https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=961bcec4-2f54-4687-915d-82fd1826a262

group_name = "预算不足通知群"
robot_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=961bcec4-2f54-4687-915d-82fd1826a262"

def wecom_notice_plan(content="我明天要跑800公里", result_text=None):
    connection = pymysql.Connect(**DB_CONFIG_PROD)
    print("连接成功!")

    now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    notice_batch_no = str(uuid.uuid4())  # e38018fb-0c80-44e7-b006-bc87e848b6065 这种格式

    sql = """
    INSERT INTO `hz_crm`.`wecom_notice_record`
    (`type`, `template_id`, `notice_batch_no`, `title`, `content`, `status`, `notice_time`, `msg_id`, `group_name`, `x_account_id`, `x_account_name`, `task_id`, `callback_time`, `robot`, `record_info`, `retry_count`, `fail_reason`, `del_flag`, `create_time`, `update_time`)
    VALUES
    (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    """

    values = (
        7,                             # type
        29,                            # template_id
        notice_batch_no,                # notice_batch_no
        '计划日报',                     # title
        content,                        # content（由参数传入）
        0,                             # status
        now,                           # notice_time（当前时间）
        None,                           # msg_id
        group_name,                     # group_name（由参数传入）
        None,                           # x_account_id
        None,                           # x_account_name
        None,                           # task_id
        None,                           # callback_time
        robot_url,                      # robot（由参数传入）
        None,                           # record_info
        1,                             # retry_count
        None,                           # fail_reason
        0,                             # del_flag
        now,                           # create_time（当前时间）
        now                            # update_time（当前时间）
    )

    try:
        with connection.cursor() as cursor:
            cursor.execute(sql, values)
            connection.commit()
            return True
    except Exception as e:
        connection.rollback()
        log_message(result_text, f"❌ 企微通知发送失败: {str(e)}", "red")
        print(f"❌ 企微通知发送失败: {str(e)}")
        return False